// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class Toast extends cc.Component {
    private static _instance: Toast = null;
    public static get_instance(): Toast {
        if (Toast._instance) {
            return Toast._instance;
        } else {
            console.error("Toast 没有初始化");
        }
    }

    public static is_init(): boolean {
        return Toast._instance != null;
    }

    text: cc.Label = null;
    onLoad() {
        Toast._instance = this;
        cc.game.addPersistRootNode(this.node);
        
        this.text = this.node.getChildByName("text").getComponent(cc.Label);

        this.node.active = false;
    }

    start() {

    }

    // update (dt) {}

    show(text: string, time = 2) {
        this.node.parent = cc.director.getScene();
        this.node.active = true;
        this.node.zIndex = 9999;

        this.text.string = text;

        this.scheduleOnce(() => {
            this.node.active = false;
        }, time);

        this.node.opacity = 0;
        cc.tween(this.node)
            .to(0.3, { opacity: 255 })
            .start();

        cc.log("toast : " + text);
    }

    /**************************************************
     * 
     **************************************************/
    public static show(text: string, time = 2) {
        if (this.is_init()) {
            Toast.get_instance().show(text, time);
        } else {
            Toast._toast(text, time);
        }
    }
    
    private static _toast(text: string, duration = 3, gravity = "BOTTOM", bg_color = cc.color(102, 102, 102, 200)) {
        // canvas
        let canvas = cc.director.getScene().getComponentInChildren(cc.Canvas);
        let width = canvas.node.width;
        let height = canvas.node.height;
        let bgNode = new cc.Node();
        // bgNode.group = "UI";

        // Lable文本格式设置
        let textNode = new cc.Node();
        let textLabel = textNode.addComponent(cc.Label);
        textLabel.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
        textLabel.verticalAlign = cc.Label.VerticalAlign.CENTER;
        textLabel.fontSize = 30;
        textLabel.string = text;

        // 当文本宽度过长时，设置为自动换行格式
        if (text.length * textLabel.fontSize > (width * 3) / 5) {
            textNode.width = (width * 3) / 5;
            textLabel.overflow = cc.Label.Overflow.RESIZE_HEIGHT;
        } else {
            textNode.width = text.length * textLabel.fontSize;
        }
        let lineCount =
            ~~((text.length * textLabel.fontSize) / ((width * 3) / 5)) + 1;
        textNode.height = textLabel.fontSize * lineCount;

        // 背景设置
        let ctx = bgNode.addComponent(cc.Graphics);
        ctx.arc(
            -textNode.width / 2,
            0,
            textNode.height / 2 + 20,
            0.5 * Math.PI,
            1.5 * Math.PI,
            true
        );
        ctx.lineTo(textNode.width / 2, -(textNode.height / 2 + 20));
        ctx.arc(
            textNode.width / 2,
            0,
            textNode.height / 2 + 20,
            1.5 * Math.PI,
            0.5 * Math.PI,
            true
        );
        ctx.lineTo(-textNode.width / 2, textNode.height / 2 + 20);
        ctx.fillColor = bg_color;
        ctx.fill();

        bgNode.addChild(textNode);

        // gravity 设置Toast显示的位置
        if (gravity === "CENTER") {
            bgNode.y = 0;
            bgNode.x = 0;
        } else if (gravity === "TOP") {
            bgNode.y = bgNode.y + (height / 5) * 2;
        } else if (gravity === "BOTTOM") {
            bgNode.y = bgNode.y - (height / 5) * 2;
        }

        bgNode.zIndex = cc.macro.MAX_ZINDEX;
        canvas.node.addChild(bgNode);


        let finished = cc.callFunc(function () {
            bgNode.destroy();
        });
        let action = cc.sequence(
            cc.moveBy(duration, cc.v2(0, 0)),
            cc.fadeOut(0.3),
            finished
        );
        bgNode.runAction(action);
    }
}
